package com.by.dao.sys.impl;

import com.by.entity.sys.ModifyUser;
import com.by.entity.sys.User;
import com.by.dao.sys.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired(required = false)
    JdbcTemplate jdbcTemplate;

    @Override
    public int save(User user) {
        String sql = "insert into s_user(id,account,password,effectiveDate,comment,islock,roleId,signUrl,empId) values(uuid(),?,md5(?),?,?,?,?,?,?)";
        return jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEffectiveDate(), user.getComment(), user.isIslock(), user.getRoleId(), user.getSignUrl(), user.getEmpId());
    }

    @Override
    public int update(User user) {
        String sql = "update s_user set effectiveDate=?,comment=?,islock=?,roleId=?,signUrl=?,empId=? where id=?";
        return jdbcTemplate.update(sql, user.getEffectiveDate(), user.getComment(), user.isIslock(), user.getRoleId(), user.getSignUrl(), user.getEmpId(), user.getId());
    }

    @Override
    public List query(Map map) {
        String sql = "SELECT u.id,u.account,u.`password`,u.effectiveDate,u.`comment`,u.islock,u.roleId,r.name roleName, u.signUrl,DATE_FORMAT( u.createDate, '%Y-%m-%d %H:%i' ) createDate,DATE_FORMAT( u.updateDate, '%Y-%m-%d %H:%i' ) updateDate,u.empId FROM s_user u  left join s_role r on u.roleId=r.id WHERE u.isdelete =0 order by u.createDate desc limit ?,?";
        int page = Integer.parseInt(map.get("page").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        int begin = (page - 1) * pageSize;
        return jdbcTemplate.queryForList(sql, begin, pageSize);
    }

    @Override
    public long queryCount(Map map) {
        String sql = "select count(1) from s_user where isdelete=0 ";
        return jdbcTemplate.queryForObject(sql, long.class);
    }


    @Override
    public int delete(String id) {
        String sql = "update s_user set isdelete=1 where id=?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int queryUser(String username) {
        String sql = "select count(1) from s_user where isdelete=0 and account like ?";
        return jdbcTemplate.queryForObject(sql, int.class, username);
    }

    @Override
    public User queryUserByAccount(String account) {
        String sql = "select u.id,u.account,u.`password`,u.islock,u.roleId from s_user u where u.isdelete=0 and  u.account like ?";
        try {
            return jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
                @Override
                public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                    return parseResult(rs);
                }
            }, account);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }

    }

    @Override
    public User queryUserById(String id) {
        String sql = "select u.id,u.account,u.`password`,u.islock,u.roleId from s_user u where u.isdelete=0 and  u.id=?";
        try {
            return jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
                @Override
                public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                    return parseResult(rs);
                }
            }, id);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    User parseResult(ResultSet rs) {
        User user = new User();
        try {
            user.setId(rs.getString("id"));
            user.setAccount(rs.getString("account"));
            user.setPassword(rs.getString("password"));
            user.setIslock(rs.getBoolean("islock"));
            user.setRoleId(rs.getString("roleId"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }


    @Override
    public int updatePwd(ModifyUser user) {
        String sql="update s_user set password=md5(?) where id=?";
        return jdbcTemplate.update(sql,user.getNewpwd(),user.getId());
    }
}
